﻿Imports System.Data
Imports System.Data.SqlClient

Namespace DB

    ''' <summary>
    ''' TypeName:    EquipmentSparepartsType
    ''' Author:      GeneratedCustomTemplate
    ''' DBTableName: STK_EquipmentSpareparts
    ''' </summary>
    ''' <remarks></remarks>

    Public Class EquipmentSparepartsType
        Inherits StockItemType

#Region "Declarations"
        Protected Friend iEquipmentSparesId As Guid
        Protected Friend iSparesStockId As Guid
        Protected Friend iEquipmentId As Guid
#End Region

#Region "Constructors"
        Public Sub New()
            MyBase.New()
            Me.iTableName = TableNamesEnum.STK_EquipmentSpareparts
        End Sub
        Public Sub New(ByVal EquipmentSparesId As Guid)
            MyBase.New()
            Me.iTableName = TableNamesEnum.STK_EquipmentSpareparts
            Me.iEquipmentSparesId = EquipmentSparesId
        End Sub
        Public Sub New(EquipmentSparesId As Guid, baseLoadType As StockItemType)
            MyBase.new(baseLoadType)
            Me.iTableName = TableNamesEnum.STK_EquipmentSpareparts
            Me.iSparesStockId = baseLoadType.StockItemId
            Me.iEquipmentSparesId = EquipmentSparesId
        End Sub
#End Region

#Region "Exposed Properties"
        Public Property EquipmentSparesId() As Guid
            Get
                Return Me.iEquipmentSparesId
            End Get
            Set(ByVal value As Guid)
                Me.iEquipmentSparesId = value
            End Set
        End Property
        Public Property SparesStockId() As Guid
            Get
                Return Me.iSparesStockId
            End Get
            Set(ByVal value As Guid)
                Me.iSparesStockId = value
            End Set
        End Property
        Public Property EquipmentId() As Guid
            Get
                Return Me.iEquipmentId
            End Get
            Set(ByVal value As Guid)
                Me.iEquipmentId = value
            End Set
        End Property
#End Region

#Region "Implementations"
        Protected Friend Overloads Function Load(ByVal Row As DataRow) As Boolean
            If IsDBNull(Row(0)) Then Me.iEquipmentSparesId = Guid.Empty Else Me.iEquipmentSparesId = Row(0)
            If IsDBNull(Row(1)) Then Me.iSparesStockId = Nothing Else Me.iSparesStockId = Row(1)
            If IsDBNull(Row(2)) Then Me.iEquipmentId = Nothing Else Me.iEquipmentId = Row(2)
            If IsDBNull(Row(3)) Then Me.iLMUserId = Nothing Else Me.iLMUserId = Row(3)
            If IsDBNull(Row(4)) Then Me.iLMDate = Nothing Else Me.iLMDate = Row(4)
            If Row.Table.Columns.Count > 5 Then
                MyBase.Load(Row, 5)
            End If
            Return True
        End Function
        Protected Friend Overrides Function Load() As Boolean
            'sp_[TableName]Get
            Dim resBool As Boolean = True
            Dim da As New SqlDataAdapter
            Dim cmd As New SqlCommand("SELECT STK_EquipmentSpareparts.*,[STK_Stock].* " & _
                                       "FROM STK_EquipmentSpareparts " & _
                                       "    INNER JOIN [STK_Stock] ON [STK_EquipmentSpareparts].[SparesStockId] = [STK_Stock].[StockItemId] " & _
                                       "WHERE STK_EquipmentSpareparts.[EquipmentSparesId]='" & Me.iEquipmentSparesId.ToString() & "'")
            cmd.Connection = Me.iConnection
            If IsNothing(Me.iTransaction) = False Then cmd.Transaction = Me.iTransaction

            Dim dt As DataTable = New DataTable
            dt = Utility.GetList(cmd)

            If dt.Rows.Count > 0 Then
                Me.Load(dt.Rows(0))
            Else
                resBool = False
            End If

            Return resBool
        End Function
        Protected Friend Overrides Function Insert() As Boolean
            'sp_[TableName]Insert
            Dim resBool As Boolean = True
            Dim resInt As Integer
            Dim da As New SqlDataAdapter

            Dim cmd As New SqlCommand("sp_" & Me.iTableName.ToString.Replace("COMP_", "") & "Insert")
            cmd.Connection = Me.iConnection
            If IsNothing(Me.iTransaction) = False Then cmd.Transaction = Me.iTransaction
            'Set command type to Stoed Procedure
            cmd.CommandType = CommandType.StoredProcedure
            'Validation or required parameters
            Try
                'Populate Stored Procedure parameters
                With cmd.Parameters
                    If IsNothing(Me.iId) Then
                        .AddWithValue("@EquipmentSparesId", System.DBNull.Value)
                    Else
                        .AddWithValue("@EquipmentSparesId", Me.iEquipmentSparesId)
                    End If
                    If IsNothing(Me.iSparesStockId) Then
                        .AddWithValue("@SparesStockId", System.DBNull.Value)
                    Else
                        .AddWithValue("@SparesStockId", Me.iSparesStockId)
                    End If
                    If IsNothing(Me.iEquipmentId) Then
                        .AddWithValue("@EquipmentId", System.DBNull.Value)
                    Else
                        .AddWithValue("@EquipmentId", Me.iEquipmentId)
                    End If
                    If IsNothing(Me.iLMUserId) Then
                        .AddWithValue("@LMUserId", System.DBNull.Value)
                    Else
                        .AddWithValue("@LMUserId", Me.iLMUserId)
                    End If
                    If IsNothing(Me.iLMDate) Then
                        .AddWithValue("@LMDate", System.DBNull.Value)
                    Else
                        .AddWithValue("@LMDate", Me.iLMDate)
                    End If

                End With
                'Execute Stored Procedure
                resInt = cmd.ExecuteNonQuery()
            Catch ex As Exception
                resBool = False
                Throw ex
            End Try
            If resInt = 0 Then
                Throw New RequestedRecordDoesNotExistException("Requested changes failed to insert, no records were modified")
            End If
            Return resBool
        End Function
        Protected Friend Overrides Function Update() As Boolean
            'sp_[TableName]Update
            Dim resBool As Boolean = True
            Dim resInt As Integer
            Dim da As New SqlDataAdapter
            Dim cmd As New SqlCommand("sp_" & Me.iTableName.ToString.Replace("COMP_", "") & "Update")
            cmd.Connection = Me.iConnection
            If IsNothing(Me.iTransaction) = False Then cmd.Transaction = Me.iTransaction
            'Set command type to Stoed Procedure
            cmd.CommandType = CommandType.StoredProcedure
            'Validation or required parameters
            Try
                'Populate Stored Procedure parameters
                With cmd.Parameters
                    If IsNothing(Me.iId) Then
                        .AddWithValue("@EquipmentSparesId", System.DBNull.Value)
                    Else
                        .AddWithValue("@EquipmentSparesId", Me.iEquipmentSparesId)
                    End If
                    If IsNothing(Me.iSparesStockId) Then
                        .AddWithValue("@SparesStockId", System.DBNull.Value)
                    Else
                        .AddWithValue("@SparesStockId", Me.iSparesStockId)
                    End If
                    If IsNothing(Me.iEquipmentId) Then
                        .AddWithValue("@EquipmentId", System.DBNull.Value)
                    Else
                        .AddWithValue("@EquipmentId", Me.iEquipmentId)
                    End If
                    If IsNothing(Me.iLMUserId) Then
                        .AddWithValue("@LMUserId", System.DBNull.Value)
                    Else
                        .AddWithValue("@LMUserId", Me.iLMUserId)
                    End If
                    If IsNothing(Me.iLMDate) Then
                        .AddWithValue("@LMDate", System.DBNull.Value)
                    Else
                        .AddWithValue("@LMDate", Me.iLMDate)
                    End If

                End With
                'Execute Stored Procedure
                resInt = cmd.ExecuteNonQuery()
            Catch ex As Exception
                resBool = False
                Throw ex
            End Try
            If resInt = 0 Then
                Throw New RequestedRecordDoesNotExistException("Requested changes failed to update, no records were modified")
            End If
            Return resBool
        End Function
        Protected Friend Overrides Function Delete() As Boolean
            'sp_[TableName]Delete
            Dim resBool As Boolean = True
            Dim resInt As Integer
            Dim da As New SqlDataAdapter
            Dim cmd As New SqlCommand("sp_" & iTableName.ToString & "Delete '" & Me.iEquipmentSparesId.ToString & "'", Me.iConnection)
            If IsNothing(Me.iTransaction) = False Then cmd.Transaction = Me.iTransaction
            Try
                resInt = cmd.ExecuteNonQuery()
            Catch ex As Exception
                resBool = False
                Throw ex
            End Try
            If resInt = 0 Then
                Throw New RequestedRecordDoesNotExistException("Requested changes failed to update, no records were modified")
            End If

            Return resBool
        End Function
        Protected Friend Overrides Function ValidateOnDelete() As Boolean
            Return True
        End Function
        Protected Friend Overrides Function ValidateOnInsert() As Boolean
            Return True
        End Function
        Protected Friend Overrides Function ValidateOnUpdate() As Boolean
            Return True
        End Function
#End Region

#Region "Overrides"
        Public Overrides Function ToString() As String
            Return MyBase.ToString
        End Function
        Public Overrides Function ToDetailsListBoxItem() As WindowsExtendedControls.DetailListBoxItem
            Return New WindowsExtendedControls.DetailListBoxItem(Me.ToString)
        End Function
#End Region

    End Class

    Public Class EquipmentSparepartsTypeCollection
        Inherits DBTableObjectCollectionBase

        Public DataFilters As New DataFilterTypeCollection(Of EquipmentSparepartsTypeFields)(Me.iTablename.ToString)
        Public DataSorters As New DataSorterTypeCollection(Of EquipmentSparepartsTypeFields)(Me.iTableName.ToString)

        Public Sub New()
            MyBase.New(TableNamesEnum.STK_EquipmentSpareparts)
        End Sub

        Default Public Property Item(ByVal Index As Integer) As EquipmentSparepartsType
            Get
                Return CType(Me.List.Item(Index), EquipmentSparepartsType)
            End Get
            Set(ByVal value As EquipmentSparepartsType)
                Me.List.Item(Index) = value
            End Set
        End Property

        Protected Friend Overrides Function Load() As Boolean
            Return Me.Load("SELECT STK_EquipmentSpareparts.*,[STK_Stock].* " & _
                           "FROM STK_EquipmentSpareparts " & _
                           "    INNER JOIN [STK_Stock] ON [STK_EquipmentSpareparts].[SparesStockId] = [STK_Stock].[StockItemId] " & Me.DataFilters.GetSQL & Me.DataSorters.GetSQL)
        End Function

        Protected Friend Overloads Function Load(ByVal CustomFilter As String) As Boolean
            'Get data from DB
            Dim cmd As New SqlCommand
            cmd.Connection = Me.iConnection
            If IsNothing(Me.iTransaction) = False Then cmd.Transaction = Me.iTransaction
            Dim dt As DataTable
            Try
                cmd.CommandText = CustomFilter
                dt = Utility.GetList(cmd)
            Catch ex As Exception
                Throw ex
            End Try
            'Clear exising list
            Dim row As DataRow
            List.Clear()

            'Build new list
            Dim typeObject As EquipmentSparepartsType

            For Each row In dt.Rows
                typeObject = New EquipmentSparepartsType()
                typeObject.iConnection = Me.iConnection
                typeObject.iTransaction = Me.iTransaction
                If typeObject.Load(row) = True Then Me.List.Add(typeObject)
            Next
            Return True
        End Function

        Public Overloads Sub Sort(ByVal propertyToSort As EquipmentSparepartsTypeFields, ByVal sortOrder As SortOrder)
            ipropertyToSort = propertyToSort.ToString
            isortOrder = sortOrder
            Me.InnerList.Sort(Me)
        End Sub

    End Class

#Region "Enumerations"       'Enumerations
    Public Enum EquipmentSparepartsTypeFields
        EquipmentSparesId
        SparesStockId
        EquipmentId
        LMUserId
        LMDate

    End Enum
#End Region

End Namespace